<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.4.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/Frog_32px_1177822_easyicon.net.ico">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/Frog_32px_1177822_easyicon.net.ico">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/Frog_16px_1177822_easyicon.net.ico">
  <link rel="mask-icon" href="/images/Frog_32px_1177822_easyicon.net.ico" color="#222">

<link rel="stylesheet" href="/css/main.css">


<link rel="stylesheet" href="/lib/font-awesome/css/all.min.css">
  <link rel="stylesheet" href="/lib/pace/pace-theme-minimal.min.css">
  <script src="/lib/pace/pace.min.js"></script>

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"hxy1997.xyz","root":"/","scheme":"Pisces","version":"7.8.0","exturl":false,"sidebar":{"position":"left","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":false,"show_result":false,"style":null},"back2top":{"enable":true,"sidebar":false,"scrollpercent":false},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":true,"pangu":false,"comments":{"style":"tabs","active":"valine","storage":true,"lazyload":true,"nav":null,"activeClass":"valine"},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"输入关键字","hits_empty":"没有找到与「${query}」相关搜索","hits_stats":"${hits} 条相关记录，共耗时 ${time} ms"}},"localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},"path":"search.json"};
  </script>

  <meta name="description" content="在学习C语言的时候，曾经写过一个能实现四则运算的计算器大作业，当年觉得云里雾里，好难，其实搞懂了逆波兰表达式的原理和巧妙利用栈这个数据结构，就可以实现，但是其中要考虑一些细节，不手写一遍根本记不得。我现在最熟悉的语言自然是js，自然选择它重复当年的思路，力扣上只有逆波兰表达式求值和包括+-和括号的，因此选择了牛客上的表达式求值来做，当然js可以用eval函数一行搞定，不过这么做没什么意义，主要是为">
<meta property="og:type" content="article">
<meta property="og:title" content="手写一个计算器">
<meta property="og:url" content="https://hxy1997.xyz/2021/04/08/%E6%89%8B%E5%86%99%E4%B8%80%E4%B8%AA%E8%AE%A1%E7%AE%97%E5%99%A8/index.html">
<meta property="og:site_name" content="hxy的博客">
<meta property="og:description" content="在学习C语言的时候，曾经写过一个能实现四则运算的计算器大作业，当年觉得云里雾里，好难，其实搞懂了逆波兰表达式的原理和巧妙利用栈这个数据结构，就可以实现，但是其中要考虑一些细节，不手写一遍根本记不得。我现在最熟悉的语言自然是js，自然选择它重复当年的思路，力扣上只有逆波兰表达式求值和包括+-和括号的，因此选择了牛客上的表达式求值来做，当然js可以用eval函数一行搞定，不过这么做没什么意义，主要是为">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20210418202633.png">
<meta property="article:published_time" content="2021-04-08T10:44:00.000Z">
<meta property="article:modified_time" content="2021-04-18T12:26:52.129Z">
<meta property="article:author" content="hxy">
<meta property="article:tag" content="JavaScript">
<meta property="article:tag" content="计算器">
<meta property="article:tag" content="栈">
<meta property="article:tag" content="逆波兰表达式">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20210418202633.png">

<link rel="canonical" href="https://hxy1997.xyz/2021/04/08/%E6%89%8B%E5%86%99%E4%B8%80%E4%B8%AA%E8%AE%A1%E7%AE%97%E5%99%A8/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>手写一个计算器 | hxy的博客</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

<link rel="alternate" href="/atom.xml" title="hxy的博客" type="application/atom+xml">
</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">hxy的博客</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
      <p class="site-subtitle" itemprop="description">Mia san Mia!</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="main-menu menu">
        <li class="menu-item menu-item-home">

    <a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a>

  </li>
        <li class="menu-item menu-item-about">

    <a href="/about/" rel="section"><i class="fa fa-user fa-fw"></i>关于</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签</a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a>

  </li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup">
        <div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div id="search-result">
  <div id="no-result">
    <i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>
  </div>
</div>

    </div>
  </div>

</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>
  <div class="reading-progress-bar"></div>

  <a href="https://github.com/huxingyi1997" class="github-corner" title="Follow me on GitHub" aria-label="Follow me on GitHub" rel="noopener" target="_blank"><svg width="80" height="80" viewBox="0 0 250 250" aria-hidden="true"><path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path><path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"></path><path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"></path></svg></a>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://hxy1997.xyz/2021/04/08/%E6%89%8B%E5%86%99%E4%B8%80%E4%B8%AA%E8%AE%A1%E7%AE%97%E5%99%A8/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/Robben.gif">
      <meta itemprop="name" content="hxy">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="hxy的博客">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          手写一个计算器
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2021-04-08 18:44:00" itemprop="dateCreated datePublished" datetime="2021-04-08T18:44:00+08:00">2021-04-08</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="far fa-calendar-check"></i>
                </span>
                <span class="post-meta-item-text">更新于</span>
                <time title="修改时间：2021-04-18 20:26:52" itemprop="dateModified" datetime="2021-04-18T20:26:52+08:00">2021-04-18</time>
              </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/%E8%AE%A1%E7%AE%97%E6%9C%BA%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86/" itemprop="url" rel="index"><span itemprop="name">计算机基础知识</span></a>
                </span>
            </span>

          
            <span class="post-meta-item" title="热度" id="busuanzi_container_page_pv" style="display: none;">
              <span class="post-meta-item-icon">
                <i class="fa fa-eye"></i>
              </span>
              <span class="post-meta-item-text">热度：</span>
              <span id="busuanzi_value_page_pv"></span>
            </span>
  
  <span class="post-meta-item">
    
      <span class="post-meta-item-icon">
        <i class="far fa-comment"></i>
      </span>
      <span class="post-meta-item-text">Valine：</span>
    
    <a title="valine" href="/2021/04/08/%E6%89%8B%E5%86%99%E4%B8%80%E4%B8%AA%E8%AE%A1%E7%AE%97%E5%99%A8/#valine-comments" itemprop="discussionUrl">
      <span class="post-comments-count valine-comment-count" data-xid="/2021/04/08/%E6%89%8B%E5%86%99%E4%B8%80%E4%B8%AA%E8%AE%A1%E7%AE%97%E5%99%A8/" itemprop="commentCount"></span>
    </a>
  </span>
  
  

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <p>在学习C语言的时候，曾经写过一个能实现四则运算的计算器大作业，当年觉得云里雾里，好难，其实搞懂了逆波兰表达式的原理和巧妙利用栈这个数据结构，就可以实现，但是其中要考虑一些细节，不手写一遍根本记不得。我现在最熟悉的语言自然是js，自然选择它重复当年的思路，力扣上只有逆波兰表达式求值和包括+-和括号的，因此选择了牛客上的<a target="_blank" rel="noopener" href="https://www.nowcoder.com/practice/c215ba61c8b1443b996351df929dc4d4">表达式求值</a>来做，当然js可以用eval函数一行搞定，不过这么做没什么意义，主要是为了复习计算机的基础知识，还是自己动手，丰衣足食。</p>
<span id="more"></span>

<h2 id="题目描述"><a href="#题目描述" class="headerlink" title="题目描述"></a>题目描述</h2><p>请写一个整数计算器，支持加减乘三种运算和括号。</p>
<p>示例1</p>
<p>输入</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">&quot;1+2&quot;</span><br></pre></td></tr></table></figure>

<p>返回值</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">3</span><br></pre></td></tr></table></figure>

<p>示例2</p>
<p>输入</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">&quot;(2*(3-4))*5&quot;</span><br></pre></td></tr></table></figure>

<p>返回</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">-10</span><br></pre></td></tr></table></figure>

<p>示例3</p>
<p>输入</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">&quot;3+2*3*4-1&quot;</span><br></pre></td></tr></table></figure>

<p>返回值</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">26</span><br></pre></td></tr></table></figure>

<h2 id="实现逆波兰表达式"><a href="#实现逆波兰表达式" class="headerlink" title="实现逆波兰表达式"></a>实现逆波兰表达式</h2><p>我选择将问题拆解成2步，完成这个问题，第一步是实现逆波兰表达式，消除所有括号。我们常用的计算表达式是中缀表达式，而逆波兰表达式是后缀表达式。</p>
<p>具体理解起来，将表达式写作二叉树的形式。例如，我们正常情况下看到的数学表达式a+b*(c-d)-e/f可以表示成如下所示的二叉树</p>
<p><img data-src="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20210418202633.png"></p>
<p>对应于这棵二叉树是中序遍历，当然建立这棵树的过程需要考虑运算符的优先级，括号的配对，栈的使用。因此常规的表达式叫作中序表达式。而按照后序遍历的后序表达式就被称作逆波兰表达式，该表达式写作abcd-*+ef/-，这样可以很方便的进行计算。</p>
<p>将一个普通的中序表达式转换为逆波兰表达式的一般算法是：</p>
<p>1、首先构造一个运算符栈，此运算符在栈内遵循越往栈顶优先级越高的原则。（&gt;*/&gt;+-</p>
<p>2、读入一个中缀表达式，要注意末尾如果是数也要读入。</p>
<p>3、从左至右扫描该中缀表达式，如果当前字符是数字，则分析到该数字串的结束，并将该数字串保存至数组。</p>
<p>4、如果不是数字，该字符则是运算符，此时需比较该运算符与运算符栈顶运算符的优先关系：</p>
<p>（1）、若该运算符优先级高于栈顶运算符优先级别（或栈为空），则直接将该运算符压入运算符栈中；</p>
<p>（2）、若该运算符优先级小于或等于此运算符栈顶的运算符，则弹出栈顶运算符并保存至数组，重复比较、输出，直到栈为空或该运算符优先级高于栈顶运算符，然后将该运算符入栈。</p>
<p>5、重复上述操作(3)-(4)直至扫描完整个简单算术表达式，确定所有字符都得到正确处理，输出结果便是中缀表达式转化为逆波兰表示的简单算术表达式。</p>
<p>具体的实现方式，需要在原先的函数中设定两个栈和一个字符串，第一个栈用来保存逆波兰表达式序列（其实后来并没有利用栈的性质，只用了数组，如果一遍操作生成最终结果的话需要利用），第二个栈用来保存运算符，即运算符栈，字符串用来保存当前数</p>
<p>首先声明这些变量</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line">&#x2F;**</span><br><span class="line"> * 代码中的类名、方法名、参数名已经指定，请勿修改，直接返回方法规定的值即可</span><br><span class="line"> * 返回表达式的值</span><br><span class="line"> * @param s string字符串 待计算的表达式</span><br><span class="line"> * @return int整型</span><br><span class="line"> *&#x2F;</span><br><span class="line">function solve( s ) &#123;</span><br><span class="line">    &#x2F;&#x2F; write code here</span><br><span class="line">    &#x2F;&#x2F; 中缀转后缀</span><br><span class="line">    let stack &#x3D; [];</span><br><span class="line">    let syStack &#x3D; [];</span><br><span class="line">    let num &#x3D; &quot;&quot;;</span><br><span class="line">&#125;</span><br><span class="line">module.exports &#x3D; &#123;</span><br><span class="line">    solve : solve</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>

<p>接下来遍历字符串，首先处理数字，使其能计算每个数的值，从这一步开始，仅保留主函数</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">function solve( s ) &#123;</span><br><span class="line">    let stack &#x3D; [];</span><br><span class="line">    let syStack &#x3D; [];</span><br><span class="line">    let num &#x3D; &quot;&quot;;</span><br><span class="line">    for (let char of s) &#123;</span><br><span class="line">        if (char &gt;&#x3D; &#39;0&#39; &amp;&amp; char &lt;&#x3D; &#39;9&#39;) &#123;</span><br><span class="line">            num +&#x3D; char;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>考虑符号的分类，其中括号&gt;乘号&gt;加减，其中括号要分成两类(和)，其中(必须入运算符栈，)必须找到对应的(并将两者之间的运算符全部出栈，注意处理符号的时候要将数保存进入第一个栈中。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br></pre></td><td class="code"><pre><span class="line">function solve( s ) &#123;</span><br><span class="line">    &#x2F;&#x2F; write code here</span><br><span class="line">    &#x2F;&#x2F; 中缀转后缀</span><br><span class="line">    let stack &#x3D; [];</span><br><span class="line">    let syStack &#x3D; [];</span><br><span class="line">    let num &#x3D; &quot;&quot;;</span><br><span class="line">    for (let char of s) &#123;</span><br><span class="line">        if (char &gt;&#x3D; &#39;0&#39; &amp;&amp; char &lt;&#x3D; &#39;9&#39;) &#123;</span><br><span class="line">            num +&#x3D; char;</span><br><span class="line">        &#125; else if (char &#x3D;&#x3D;&#x3D; &#39;(&#39;) &#123;</span><br><span class="line">            if (num !&#x3D;&#x3D; &#39;&#39;) &#123;</span><br><span class="line">                stack.push(num);</span><br><span class="line">                num &#x3D; &#39;&#39;;</span><br><span class="line">            &#125;</span><br><span class="line">            syStack.push(char);</span><br><span class="line">        &#125; else if (char &#x3D;&#x3D;&#x3D; &#39;+&#39; || char &#x3D;&#x3D;&#x3D; &#39;-&#39;) &#123;</span><br><span class="line">            if (num !&#x3D;&#x3D; &#39;&#39;) &#123;</span><br><span class="line">                stack.push(num);</span><br><span class="line">                num &#x3D; &#39;&#39;;</span><br><span class="line">            &#125;</span><br><span class="line">            while (syStack.length &amp;&amp; syStack[syStack.length - 1] !&#x3D;&#x3D; &#39;(&#39;) &#123;</span><br><span class="line">                stack.push(syStack.pop());</span><br><span class="line">            &#125;</span><br><span class="line">            syStack.push(char);</span><br><span class="line">        &#125; else if (char &#x3D;&#x3D;&#x3D; &#39;*&#39;) &#123;</span><br><span class="line">            if (num !&#x3D;&#x3D; &#39;&#39;) &#123;</span><br><span class="line">                stack.push(num);</span><br><span class="line">                num &#x3D; &#39;&#39;;</span><br><span class="line">            &#125;</span><br><span class="line">            while (syStack.length &amp;&amp; syStack[syStack.length - 1] &#x3D;&#x3D;&#x3D; &#39;*&#39;) &#123;</span><br><span class="line">                stack.push(syStack.pop());</span><br><span class="line">            &#125;</span><br><span class="line">            syStack.push(char);</span><br><span class="line">        &#125; else if (char &#x3D;&#x3D;&#x3D; &#39;)&#39;) &#123;</span><br><span class="line">            if (num !&#x3D;&#x3D; &#39;&#39;) &#123;</span><br><span class="line">                stack.push(num);</span><br><span class="line">                num &#x3D; &#39;&#39;;</span><br><span class="line">            &#125;</span><br><span class="line">            while (syStack.length &amp;&amp; syStack[syStack.length - 1] !&#x3D;&#x3D; &#39;(&#39;) &#123;</span><br><span class="line">                stack.push(syStack.pop());</span><br><span class="line">            &#125;</span><br><span class="line">            syStack.pop();</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>这里要注意最后一个如果是数需要再次保存，如果还有符号在运算符栈中需要出栈操作</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br></pre></td><td class="code"><pre><span class="line">function solve( s ) &#123;</span><br><span class="line">    &#x2F;&#x2F; write code here</span><br><span class="line">    &#x2F;&#x2F; 中缀转后缀</span><br><span class="line">    let stack &#x3D; [];</span><br><span class="line">    let syStack &#x3D; [];</span><br><span class="line">    let num &#x3D; &quot;&quot;;</span><br><span class="line">    for (let char of s) &#123;</span><br><span class="line">        if (char &gt;&#x3D; &#39;0&#39; &amp;&amp; char &lt;&#x3D; &#39;9&#39;) &#123;</span><br><span class="line">            num +&#x3D; char;</span><br><span class="line">        &#125; else if (char &#x3D;&#x3D;&#x3D; &#39;(&#39;) &#123;</span><br><span class="line">            if (num !&#x3D;&#x3D; &#39;&#39;) &#123;</span><br><span class="line">                stack.push(num);</span><br><span class="line">                num &#x3D; &#39;&#39;;</span><br><span class="line">            &#125;</span><br><span class="line">            syStack.push(char);</span><br><span class="line">        &#125; else if (char &#x3D;&#x3D;&#x3D; &#39;+&#39; || char &#x3D;&#x3D;&#x3D; &#39;-&#39;) &#123;</span><br><span class="line">            if (num !&#x3D;&#x3D; &#39;&#39;) &#123;</span><br><span class="line">                stack.push(num);</span><br><span class="line">                num &#x3D; &#39;&#39;;</span><br><span class="line">            &#125;</span><br><span class="line">            while (syStack.length &amp;&amp; syStack[syStack.length - 1] !&#x3D;&#x3D; &#39;(&#39;) &#123;</span><br><span class="line">                stack.push(syStack.pop());</span><br><span class="line">            &#125;</span><br><span class="line">            syStack.push(char);</span><br><span class="line">        &#125; else if (char &#x3D;&#x3D;&#x3D; &#39;*&#39;) &#123;</span><br><span class="line">            if (num !&#x3D;&#x3D; &#39;&#39;) &#123;</span><br><span class="line">                stack.push(num);</span><br><span class="line">                num &#x3D; &#39;&#39;;</span><br><span class="line">            &#125;</span><br><span class="line">            while (syStack.length &amp;&amp; syStack[syStack.length - 1] &#x3D;&#x3D;&#x3D; &#39;*&#39;) &#123;</span><br><span class="line">                stack.push(syStack.pop());</span><br><span class="line">            &#125;</span><br><span class="line">            syStack.push(char);</span><br><span class="line">        &#125; else if (char &#x3D;&#x3D;&#x3D; &#39;)&#39;) &#123;</span><br><span class="line">            if (num !&#x3D;&#x3D; &#39;&#39;) &#123;</span><br><span class="line">                stack.push(num);</span><br><span class="line">                num &#x3D; &#39;&#39;;</span><br><span class="line">            &#125;</span><br><span class="line">            while (syStack.length &amp;&amp; syStack[syStack.length - 1] !&#x3D;&#x3D; &#39;(&#39;) &#123;</span><br><span class="line">                stack.push(syStack.pop());</span><br><span class="line">            &#125;</span><br><span class="line">            syStack.pop();</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    if (num !&#x3D;&#x3D; &#39;&#39;) &#123;</span><br><span class="line">        stack.push(num);</span><br><span class="line">    &#125;</span><br><span class="line">    while (syStack.length) &#123;</span><br><span class="line">        stack.push(syStack.pop());</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="逆波兰表达式计算"><a href="#逆波兰表达式计算" class="headerlink" title="逆波兰表达式计算"></a>逆波兰表达式计算</h2><p>有了逆波兰表达式树求值就非常简单了，想象一下刚才提到的二叉树，先求左子树的值，再求右子树值，然后根据根节点的运算符逐步计算最终的值。需要再创建一个栈模拟逆波兰表达式，遇见数进行入栈，遇见符号将栈中最近的两个数出栈进行操作后再入栈，最后就剩下一个数，就是最后的结果</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line">function solve( s ) &#123;</span><br><span class="line">    let calStack &#x3D; [];</span><br><span class="line">    for (let ele of stack) &#123;</span><br><span class="line">        if (ele &#x3D;&#x3D;&#x3D; &#39;+&#39; || ele &#x3D;&#x3D;&#x3D; &#39;-&#39; || ele &#x3D;&#x3D;&#x3D; &#39;*&#39;) &#123;</span><br><span class="line">            let num2 &#x3D; calStack.pop();</span><br><span class="line">            let num1 &#x3D; calStack.pop();</span><br><span class="line">            let res;</span><br><span class="line">            switch (ele) &#123;</span><br><span class="line">                case &#39;+&#39;:</span><br><span class="line">                    res &#x3D; num1 + num2;</span><br><span class="line">                    break;</span><br><span class="line">                case &#39;-&#39;:</span><br><span class="line">                    res &#x3D; num1 - num2;</span><br><span class="line">                    break;</span><br><span class="line">                case &#39;*&#39;:</span><br><span class="line">                    res &#x3D; num1 * num2;</span><br><span class="line">                    break;</span><br><span class="line">            &#125;</span><br><span class="line">            calStack.push(res);</span><br><span class="line">        &#125; else &#123;</span><br><span class="line">            calStack.push(parseInt(ele));</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    return calStack[0];</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="完整代码"><a href="#完整代码" class="headerlink" title="完整代码"></a>完整代码</h2><p>将两个步骤组合起来，可以实现最后的结果了</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br></pre></td><td class="code"><pre><span class="line">&#x2F;**</span><br><span class="line"> * 代码中的类名、方法名、参数名已经指定，请勿修改，直接返回方法规定的值即可</span><br><span class="line"> * 返回表达式的值</span><br><span class="line"> * @param s string字符串 待计算的表达式</span><br><span class="line"> * @return int整型</span><br><span class="line"> *&#x2F;</span><br><span class="line">function solve( s ) &#123;</span><br><span class="line">    &#x2F;&#x2F; write code here</span><br><span class="line">    &#x2F;&#x2F; 中缀转后缀</span><br><span class="line">    let stack &#x3D; [];</span><br><span class="line">    let syStack &#x3D; [];</span><br><span class="line">    let num &#x3D; &quot;&quot;;</span><br><span class="line">    for (let char of s) &#123;</span><br><span class="line">        if (char &gt;&#x3D; &#39;0&#39; &amp;&amp; char &lt;&#x3D; &#39;9&#39;) &#123;</span><br><span class="line">            num +&#x3D; char;</span><br><span class="line">        &#125; else if (char &#x3D;&#x3D;&#x3D; &#39;(&#39;) &#123;</span><br><span class="line">            if (num !&#x3D;&#x3D; &#39;&#39;) &#123;</span><br><span class="line">                stack.push(num);</span><br><span class="line">                num &#x3D; &#39;&#39;;</span><br><span class="line">            &#125;</span><br><span class="line">            syStack.push(char);</span><br><span class="line">        &#125; else if (char &#x3D;&#x3D;&#x3D; &#39;+&#39; || char &#x3D;&#x3D;&#x3D; &#39;-&#39;) &#123;</span><br><span class="line">            if (num !&#x3D;&#x3D; &#39;&#39;) &#123;</span><br><span class="line">                stack.push(num);</span><br><span class="line">                num &#x3D; &#39;&#39;;</span><br><span class="line">            &#125;</span><br><span class="line">            while (syStack.length &amp;&amp; syStack[syStack.length - 1] !&#x3D;&#x3D; &#39;(&#39;) &#123;</span><br><span class="line">                stack.push(syStack.pop());</span><br><span class="line">            &#125;</span><br><span class="line">            syStack.push(char);</span><br><span class="line">        &#125; else if (char &#x3D;&#x3D;&#x3D; &#39;*&#39;) &#123;</span><br><span class="line">            if (num !&#x3D;&#x3D; &#39;&#39;) &#123;</span><br><span class="line">                stack.push(num);</span><br><span class="line">                num &#x3D; &#39;&#39;;</span><br><span class="line">            &#125;</span><br><span class="line">            while (syStack.length &amp;&amp; syStack[syStack.length - 1] &#x3D;&#x3D;&#x3D; &#39;*&#39;) &#123;</span><br><span class="line">                stack.push(syStack.pop());</span><br><span class="line">            &#125;</span><br><span class="line">            syStack.push(char);</span><br><span class="line">        &#125; else if (char &#x3D;&#x3D;&#x3D; &#39;)&#39;) &#123;</span><br><span class="line">            if (num !&#x3D;&#x3D; &#39;&#39;) &#123;</span><br><span class="line">                stack.push(num);</span><br><span class="line">                num &#x3D; &#39;&#39;;</span><br><span class="line">            &#125;</span><br><span class="line">            while (syStack.length &amp;&amp; syStack[syStack.length - 1] !&#x3D;&#x3D; &#39;(&#39;) &#123;</span><br><span class="line">                stack.push(syStack.pop());</span><br><span class="line">            &#125;</span><br><span class="line">            syStack.pop();</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    if (num !&#x3D;&#x3D; &#39;&#39;) &#123;</span><br><span class="line">        stack.push(num);</span><br><span class="line">    &#125;</span><br><span class="line">    while (syStack.length) &#123;</span><br><span class="line">        stack.push(syStack.pop());</span><br><span class="line">    &#125;</span><br><span class="line">    let calStack &#x3D; [];</span><br><span class="line">    for (let ele of stack) &#123;</span><br><span class="line">        if (ele &#x3D;&#x3D;&#x3D; &#39;+&#39; || ele &#x3D;&#x3D;&#x3D; &#39;-&#39; || ele &#x3D;&#x3D;&#x3D; &#39;*&#39;) &#123;</span><br><span class="line">            let num2 &#x3D; calStack.pop();</span><br><span class="line">            let num1 &#x3D; calStack.pop();</span><br><span class="line">            let res;</span><br><span class="line">            switch (ele) &#123;</span><br><span class="line">                case &#39;+&#39;:</span><br><span class="line">                    res &#x3D; num1 + num2;</span><br><span class="line">                    break;</span><br><span class="line">                case &#39;-&#39;:</span><br><span class="line">                    res &#x3D; num1 - num2;</span><br><span class="line">                    break;</span><br><span class="line">                case &#39;*&#39;:</span><br><span class="line">                    res &#x3D; num1 * num2;</span><br><span class="line">                    break;</span><br><span class="line">            &#125;</span><br><span class="line">            calStack.push(res);</span><br><span class="line">        &#125; else &#123;</span><br><span class="line">            calStack.push(parseInt(ele));</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    return calStack[0];</span><br><span class="line">&#125;</span><br><span class="line">module.exports &#x3D; &#123;</span><br><span class="line">    solve : solve</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>


    </div>

    
    
    
      
<div>
        <div style="text-align:center;color: #ccc;font-size:14px;">-------------本文结束<i class="fa fa-paw"></i>感谢您的阅读-------------</div>
</div>
        

  <div class="followme">
    <p>欢迎关注我的其它发布渠道</p>

    <div class="social-list">

        <div class="social-item">
          <a target="_blank" class="social-link" href="/atom.xml">
            <span class="icon">
              <i class="fa fa-rss"></i>
            </span>

            <span class="label">RSS</span>
          </a>
        </div>
    </div>
  </div>


      <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/JavaScript/" rel="tag"><i class="fa fa-tag"></i> JavaScript</a>
              <a href="/tags/%E8%AE%A1%E7%AE%97%E5%99%A8/" rel="tag"><i class="fa fa-tag"></i> 计算器</a>
              <a href="/tags/%E6%A0%88/" rel="tag"><i class="fa fa-tag"></i> 栈</a>
              <a href="/tags/%E9%80%86%E6%B3%A2%E5%85%B0%E8%A1%A8%E8%BE%BE%E5%BC%8F/" rel="tag"><i class="fa fa-tag"></i> 逆波兰表达式</a>
          </div>

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/2021/04/07/Typescript%E9%80%9F%E6%88%90%E8%87%AA%E5%AD%A6/" rel="prev" title="Typescript速成自学">
      <i class="fa fa-chevron-left"></i> Typescript速成自学
    </a></div>
      <div class="post-nav-item">
    <a href="/2021/04/13/40%E9%81%93this%E9%9D%A2%E8%AF%95%E9%A2%98+%E6%89%8B%E5%86%99new,call,apply,bind/" rel="next" title="40道this面试题+手写new,call,apply,bind">
      40道this面试题+手写new,call,apply,bind <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          
    <div class="comments" id="valine-comments"></div>

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E9%A2%98%E7%9B%AE%E6%8F%8F%E8%BF%B0"><span class="nav-text">题目描述</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%AE%9E%E7%8E%B0%E9%80%86%E6%B3%A2%E5%85%B0%E8%A1%A8%E8%BE%BE%E5%BC%8F"><span class="nav-text">实现逆波兰表达式</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E9%80%86%E6%B3%A2%E5%85%B0%E8%A1%A8%E8%BE%BE%E5%BC%8F%E8%AE%A1%E7%AE%97"><span class="nav-text">逆波兰表达式计算</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%AE%8C%E6%95%B4%E4%BB%A3%E7%A0%81"><span class="nav-text">完整代码</span></a></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="hxy"
      src="/images/Robben.gif">
  <p class="site-author-name" itemprop="name">hxy</p>
  <div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">80</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/categories/">
          
        <span class="site-state-item-count">8</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/tags/">
          
        <span class="site-state-item-count">120</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author motion-element">
      <span class="links-of-author-item">
        <a href="https://github.com/huxingyi1997" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;huxingyi1997" rel="noopener" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
      <span class="links-of-author-item">
        <a href="mailto:huxingyi1997@zju.edu.cn" title="E-Mail → mailto:huxingyi1997@zju.edu.cn" rel="noopener" target="_blank"><i class="fa fa-envelope fa-fw"></i>E-Mail</a>
      </span>
  </div>



      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2022</span>
  <span class="with-love">
    <i class="fa fa-frog"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">hxy</span>
</div>

<div class="theme-info">
  <div class="powered-by"></div>
  <span class="post-count">博客全站共1039.2k字</span>
</div>

        
<div class="busuanzi-count">
  <script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
    <span class="post-meta-item" id="busuanzi_container_site_uv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="fa fa-user"></i>
      </span>
      <span class="site-uv" title="总访客量">
        <span id="busuanzi_value_site_uv"></span>
      </span>
    </span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item" id="busuanzi_container_site_pv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="fa fa-eye"></i>
      </span>
      <span class="site-pv" title="总访问量">
        <span id="busuanzi_value_site_pv"></span>
      </span>
    </span>
</div>








      </div>
    </footer>
  </div>

  
  <script src="/lib/anime.min.js"></script>
  <script src="//cdn.jsdelivr.net/npm/lozad@1/dist/lozad.min.js"></script>
  <script src="/lib/velocity/velocity.min.js"></script>
  <script src="/lib/velocity/velocity.ui.min.js"></script>

<script src="/js/utils.js"></script>

<script src="/js/motion.js"></script>


<script src="/js/schemes/pisces.js"></script>


<script src="/js/next-boot.js"></script>




  




  
<script src="/js/local-search.js"></script>













  

  


<script>
NexT.utils.loadComments(document.querySelector('#valine-comments'), () => {
  NexT.utils.getScript('//unpkg.com/valine/dist/Valine.min.js', () => {
    var GUEST = ['nick', 'mail', 'link'];
    var guest = 'nick,mail,link';
    guest = guest.split(',').filter(item => {
      return GUEST.includes(item);
    });
    new Valine({
      el         : '#valine-comments',
      verify     : false,
      notify     : true,
      appId      : 'pQsO3ySbU4VtWN2j1FLA74Ha-gzGzoHsz',
      appKey     : 'QYacMDY2VY7Wazprg1X6FiUv',
      placeholder: "Just go go",
      avatar     : 'mm',
      meta       : guest,
      pageSize   : '10' || 10,
      visitor    : false,
      lang       : 'zh-cn' || 'zh-cn',
      path       : location.pathname,
      recordIP   : false,
      serverURLs : ''
    });
  }, window.Valine);
});
</script>

  
  <!-- 动态背景特效 -->
  <!-- 樱花特效 -->
    <script async src="/js/src/sakura.js"></script>
    <script async src="/js/src/fairyDustCursor.js"></script>
</body>
</html>
